FillMemory
FillMemory Address, FillRuns, FillPattern, FillType
 
Parameters:

    Address = The memory address you wish to start filling at
    FillRuns = The number fills using this pattern
    FillPattern = The data pattern you wish to fill with
    FillType = The data pattern width in bytes to use (1,2,3,4) combined with fill mode (Write/AND/OR/XOR)
Returns: NONE
 

     FillMemory is used to fill memory with a pattern. The Pattern can be in bytes, words, triplets or longs. The filltype parameter for this function also includes support for binary operation modes, from normal write over mode (default) through to AND, OR and XOR modes. To select the mode you use the required bit flag to enable it.


Fill Types Patten Widths

      The bottom 3 bits of the FillTYPE parameter are used to tell the filler what pattern width you require. The routine can fill in Bytes, Words, Triplets or Longs. If you need to fill large chunks of memory its best to use the 32bit integers/longs for the best performance.


1= Fill is writing bytes. (FillSize=1*FillRuns)
2= Fill is writing words. (FillSize=2*FillRuns)
3= Fill is writing triplets. (FillSize=3*FillRuns)
4= Fill is writing integer(longs). (FillSize=4*FillRuns)




Fill Types Write Modes

      By default FillMemory will used write over mode when filling memory. So the data pattern you give it, will be written directly to memory as is. Sometimes though might need to perform bitwise opterations to memory, so we're include such functionality in this command for added performance. To enable a mode just add the Pattern Width value above to the any one of these write mode values.


8= (bit3) Enable AND write mode when filling memory.
16= (bit4) Enable OR write mode.
32= (bit5) Enable XOR write mode.




FACTS:


      * FillMemory is dealing with raw memory, as such, PlayBASIC can not protect you from making a mistake. Thus FillMemory assumes the addresses and sizes you give it are valid, if not, you should expect your program to crash !

      * XOR mode can be handy for people wanting to hide information in data files. So what you would do is take your original information and XOR that data with a KEY value before you save it out to disc. When you reload the data you XOR it again with the same key to restore it it's original state.




Mini Tutorial #1:


      This example demos filling a bank with various patterns byte patterns and pattern widths. The filling routine in this demo are all using the the default write over mode.

  
  
; The Size of our test bank in bytes
  Size=24
  
; create bank #1
  CreateBank 1,Size
  
; ==========================
; Fill bank with BYTE $AA
; ==========================
; Setting Pattern Width make it Write the
; pattern as BYTES
  FillMemory GetBankPtr(1),Size,$AA,1
  
  ShowBank(1)
  
  
; ==========================
; Fill bank with WORD $AABB
; ==========================
; Setting Pattern Width make it Write the
; pattern as WORDS
  FillMemory GetBankPtr(1),Size/2,$AABB,2
  
  ShowBank(1)
  
  
; ==========================
; Fill bank with 24bit $AAbbCC
; ==========================
; Setting Pattern Width make it Write the
; pattern as byte triplets
  FillMemory GetBankPtr(1),(Size/3)-1,$AABBCC,3
  
  ShowBank(1)
  
  
  
; ==========================
; Fill bank with 32bit $AAbbCCDD
; ==========================
; Setting Pattern Width make it Write the
; pattern as LONGs
  FillMemory GetBankPtr(1),(Size/4),$AABBCCDD,4
  
  ShowBank(1)
  
  
  
  
;refresh the screen and wait for a key press
  Sync
  WaitKey
  
  
  
  
  
Function ShowBank(Index)
  Print "Bank Contents"
  
  Size=GetBankSize(Index)-1
; Show the bytes from this bank
  For lp=0 To Size
     c$=Right$(Hex$(PeekBankByte(index,lp)),2)
     If lp<>size
        r$=r$+c$+","
     Else
        r$=r$+c$
     EndIf
  Next
  Print r$
  Print ""
  
; Clear the bank
  FillMemory GetBankPtr(Index),Size+1,$0,1
  
EndFunction
  




This example would output.

  
  Bank Contents
  AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA,AA
  
  Bank Contents
  BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA,BB,AA
  
  Bank Contents
  CC,BB,AA,CC,BB,AA,CC,BB,AA,CC,BB,AA,CC,BB,AA,CC,BB,AA,CC,BB,AA,00,00,00
  
  Bank Contents
  DD,CC,BB,AA,DD,CC,BB,AA,DD,CC,BB,AA,DD,CC,BB,AA,DD,CC,BB,AA,DD,CC,BB,AA
  





Mini Tutorial #2:


      This example demos filling a bank a simple byte pattern in write mode (default output mode). The exmaple then maks the data with the AND, OR and XOR modes. Since all the bytes are the same in the test bank, each byte in the output ends up the same.


  
; This example shows WRITE MODE with AND , OR & XOR fill modes
  
; The Size of our test bank in bytes
  Size=24
  
; create bank #1
  CreateBank 1,Size
  
  Address=GetBankPtr(1)
  
; ==========================
; Fill bank with BYTE $AA
; ==========================
; Setting Pattern Width make it Write the
; pattern as BYTES
; write bytes to memory
  FillMemory Address,Size,$AB,1
  
  ShowBank(1)
  
  
  
; ==========================
; Fill bank with BYTE $F0 using AND mode.
; So each byte computed NewByte=OldByte and $F0
; ==========================
; Setting Pattern Width make it Write the
; pattern as BYTES
; AND this fill pattern byte to memory
  FillMemory Address,Size,$f0,1+8
  
  ShowBank(1)
  
  
  
  
; ==========================
; Fill bank with BYTE $0E using OR mode.
; So each byte is computed NewByte=OldByte OR $0E
; ==========================
; Setting Pattern Width make it Write the
; pattern as BYTES
; OR this fill pattern byte to memory
  FillMemory Address,Size,$0e,1+16
  
  ShowBank(1)
  
  
; ==========================
; Fill bank with BYTE $FF using XOR mode.
; So each byte is computed NewByte=OldByte XOR $FF
; ==========================
; Setting Pattern Width make it Write the
; pattern as BYTES
; XOR this fill pattern byte to memory
  FillMemory Address,Size,$FF,1+32
  
  ShowBank(1)
  
; --------------------------------------------
; refresh the screen and wait for a key press
; --------------------------------------------
  Sync
  WaitKey
  
  
  
  
  
Function ShowBank(Index)
  Print "Bank Contents"
  
  Size=GetBankSize(Index)-1
; Show the bytes from this bank
  For lp=0 To Size
     c$=Right$(Hex$(PeekBankByte(index,lp)),2)
     If lp<>size
        r$=r$+c$+","
     Else
        r$=r$+c$
     EndIf
  Next
  Print r$
  Print ""
  
EndFunction
  
  



This example would output.

  
  Bank Contents
  AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB,AB
  
  Bank Contents
  A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0,A0
  
  Bank Contents
  AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE,AE
  
  Bank Contents
  51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51,51
  




 
Example Source: Download This Example
; This example encodes a message using XOR fill mode
  
  Message$="My Secret Message"
  
  
; The Size of our test bank in bytes
  Size=Len(Message$)+1
  
; create bank #1
  CreateBank 1,Size
  
; store the original message into this bank
  PokeBankString 1,0,Message$,0
  
; show the bank
  ShowBank(1)
  
  
  
; XOR to encode the bank data with our key of $4B
  FillMemory GetBankPtr(1),Size,$4B,1+32
  
; show the bank to screen
  ShowBank(1)
  
; read the bytes in the bank back as string..
  EncodedMessage$=PeekBankString(1,0,size)
  
  
; USE XOR with a key of $4B again to restore the bank
; data to it's original state
  FillMemory GetBankPtr(1),Size,$4B,1+32
  
; read the bytes in the bank back as string..
  DecodedMessage$=PeekBankString(1,0,size)
  
; show the bank to screen
  ShowBank(1)
  
  
  Ink -1
  Print "Original Message"
  Ink $ff00
  Print Message$
  Print ""
  
  Ink -1
  Print "Encoded Message"
  Ink $ff0000
  Print  EncodedMessage$
  Print ""
  
  Ink -1
  Print "Decoded Message"
  Ink $ff00
  Print  DecodedMessage$
  Print ""
  
  Ink -1
  Print ""
  
  
  
  
  
; --------------------------------------------
; refresh the screen and wait for a key press
; --------------------------------------------
  Sync
  WaitKey
  
  
  
  
  
Function ShowBank(Index)
  Print "Bank Contents"
  
  Size=GetBankSize(Index)-1
; Show the bytes from this bank
  
  For lp=0 To Size
     c$=Right$(Hex$(PeekBankByte(index,lp)),2)
     If lp<>size
        r$=r$+c$+","
     Else
        r$=r$+c$
     EndIf
  Next
  Print r$
  Print ""
  
EndFunction
  
  
  
  
  
  
  
  
  
  
 
Related Info: CopyMemory | FindMemoryRunLength | GetBankPtr | Pointer :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com